<?php
namespace Home\Controller;
use Think\Controller;

class userController extends Controller {

    public function index(){
        $this->assign('isPC', json_encode(isPC()));
        $this->show(render($this->fetch()));
    }

    public function login(){
        if(C('local')){
            cookie('openid', '123456');
            cookie('nickname', 'windroid');
            cookie('figureurl', 'http://q.qlogo.cn/qqapp/101325623/CEA15897C6C57DBD38C2D51AB5BCA39E/40');
        }
        session('access_token', cookie('access_token'));
        session('openid', cookie('openid'));
        session('nickname', cookie('nickname'));
        session('figureurl', cookie('figureurl'));
        session('gender', cookie('gender'));

        $data['access_token'] = session('access_token');
        $data['openid'] = session('openid');
        $data['nickname'] = session('nickname');
        $data['figureurl'] = session('figureurl');
        $data['gender'] = session('gender');
        $condition['openid'] = session('openid');
        if(M('user')->where($condition)->select() != NULL){
            M('user')->where($condition)->save($data);
        }else {
            M('user')->add($data);
        }
        $this->redirect('Index/index');
    }

    public function collect(){
        if(IS_AJAX){
            if(session('openid') == null){
                $this->ajaxReturn('error');
            }
            $answerId = I('post.answerId');
            if($answerId == null){
                $this->ajaxReturn('error');
            }
            $data['openid'] = session('openid');
            $data['answerId'] = $answerId;
            if(count(M('usercollection')->where($data)->select()) != 0){
                $this->ajaxReturn('collected');
            }
            M('usercollection')->add($data);
            $this->ajaxReturn('success');
        }
    }

    public function collectState(){
        if(IS_AJAX){
            if(session('openid') == null){
                $this->ajaxReturn('error');
            }
            $answerId = I('post.answerId');
            $condition['openid'] = session('openid');
            $condition['answerId'] = $answerId;

            $res = M('usercollection')->where($condition)->select();

            $this->ajaxReturn(count($res));
        }
    }

    public function getCollectList(){
        if(IS_AJAX) {
            $openid = I('post.openid');
            $res = D('usercollection')->getCollection($openid);
            $this->ajaxReturn($res);
        }
    }
}
?>